import {configApp} from '../../../utils/config'
import commonAPI from '../../../api/api'
import {numberToFixed} from "../../../utils/util"
Page({
  data: {
    hostUserInfo: {},// 雷主信息
    currInfo:{},
    voiceResultId:'',
    pageUrl:'',
    qrcodeurl: '',
    myRankingNum:'',// 擂台排名
    rankingList:[]
  },

  onLoad(options) {
    var that = this;
     wx.hideShareMenu(); // 禁止分享
    this.doLoadFontFace();// 下载字体    
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', {});
    eventChannel.emit('someEvent', {});
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      wx.getStorage({
        key: 'rankingObj',
        success(res) {
          debugger
          that.initData(data.voiceResultId,data.pageUrl,res.data);
        }
      })
      that.computeMyRankingNum();// 计算排名
    })
  },

  initData(voiceResultId,pageUrl,rankingObj){
    let that = this;
    commonAPI.arenaInvitationCard({voiceResultId,pageUrl
    }).then(r => {
      if (r.code == '200') {
        if(typeof rankingObj.myVoiceInfo.voiceResultInfo == 'string'){
          rankingObj.myVoiceInfo.voiceResultInfo = JSON.parse(rankingObj.myVoiceInfo.voiceResultInfo);// 雷主跟读信息
        }
        rankingObj.myVoiceInfo.voiceResultInfo.result.pron_fluency =  numberToFixed(rankingObj.myVoiceInfo.voiceResultInfo.result.pron_fluency)
        rankingObj.myVoiceInfo.voiceResultInfo.result.pron_completion =  numberToFixed(rankingObj.myVoiceInfo.voiceResultInfo.result.pron_completion)
        rankingObj.myVoiceInfo.voiceResultInfo.result.pron_accuracy =  numberToFixed(rankingObj.myVoiceInfo.voiceResultInfo.result.pron_accuracy)
        that.setData({
          hostUserInfo:rankingObj.myVoiceInfo,// 雷主信息
          currInfo:r.data.myVoiceInfo,// 进入者的信息
          rankingList: rankingObj.rankingList,
          qrcodeurl:configApp.img_base_url+r.data.myVoiceInfo.arenaQrcodeUrl
        });

      } else {
        FN.Toast(res.message)
      }
    })
  },
  
  doLoadFontFace() {
    wx.loadFontFace({
      family: "Monaco",
      source: 'url("https://minapp.leshijiewx.com/common/Monaco.woff")',// https://fonts.cdnfonts.com/s/14106/Monaco.woff
      scopes: ['webview', 'native'],
      success: (res) => {
          console.log('success', res.status)
          this.render()
      },
      fail: function (res) {
          console.error(res)
      },
      complete(res) {
        console.log('111', res)
      }
    });
  },

  // 计算排名
  computeMyRankingNum(){
    const that = this;
    const index = this.data.rankingList.findIndex(res=>res.openId == this.data.currInfo.openId)
    console.log('计算排名',index+1)
    this.setData({
      myRankingNum:index+1
    })
  },

     // 返回
     gohome: function () {
      wx.navigateBack({
        delta: 1
      })
    },

    tap() {
      debugger
      wx.loadFontFace({
        family: "Monaco",
        source: 'url("https://minapp.leshijiewx.com/common/Monaco.woff")',// https://fonts.cdnfonts.com/s/14106/Monaco.woff
        scopes: ['webview', 'native'], 
        success: (res) => {
            console.log('success', res.status)
            this.render()
        },
        fail: function (res) {
            console.error(res)
        },
        complete(res) {
          console.log('111', res)
        }
      });
  
      this.createSelectorQuery().select("#view4snapshot")
        .node().exec(res => {
          const node = res[0].node
          node.takeSnapshot({
            // type: 'file' 且 format: 'png' 时，可直接导出成临时文件
            type: 'arraybuffer',
            format: 'png',
            success: (res) => {
              const f = `${wx.env.USER_DATA_PATH}/hello.png`
              const fs = wx.getFileSystemManager();
              fs.writeFileSync(f, res.data, 'binary')
              wx.showToast({
                title: '保存成功'
              })
  
              wx.saveImageToPhotosAlbum({
                filePath: f,
                complete(res) {
                  console.log("saveImageToPhotosAlbum:", res)
                }
              })
            },
            fail(res) {
              console.log("takeSnapshot fail:", res)
            }
          })
        })
    },

   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var that = this;
    // app.setPageTabBarBadge(); //实时更新主菜单我的消息数字
    // app.callback = res=>{
    //   that.setData({
    //     currInfo: res.userInfo
    //   })
    // }
  },
})
